﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using System.Resources;
using System.Net.Mail;

using EHTWebsite.Code;

public partial class Controls_ContactControl : System.Web.UI.UserControl
{
    private Boolean IsVN = true;
    String languageCode = "vi-VN";
    private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            languageCode = Session[HangSo.LANGUAGE_CODE].ToString();

            if (languageCode == "en-us")
            {
                IsVN = false;
            }

        }
        catch (Exception ex)
        {
        }

        if (!Page.IsPostBack)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
            LoadLanguage(Thread.CurrentThread.CurrentCulture);
        }
    }

    public void LoadLanguage(CultureInfo ci)
    {
        this.ltrContactMessage.Text = rm.GetString("ltrContactMessage", ci);
        this.ltrDiaChi.Text = rm.GetString("ltrDiaChi_Title", ci);
        this.ltrDienThoai.Text = rm.GetString("ltrDienThoai_Title", ci);
        this.ltrHoTen.Text = rm.GetString("ltrHoTen_Title", ci);
        this.ltrMaSo.Text = rm.GetString("ltrMaSo_Title", ci);
        this.btnSend.Text = rm.GetString("btnSend_Title", ci);
        this.btnReset.Text = rm.GetString("btnReset_Title", ci);
        this.ltrNoiDung.Text = rm.GetString("ltrNoiDung_Title", ci);
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        Page.Validate();

        if (Page.IsValid)
        {
            EHTWebsite.Code.LienHe theLienHe = new EHTWebsite.Code.LienHe();
            theLienHe.DiaChi = this.txtAddress.Text.Trim();
            theLienHe.DienThoai = this.txtPhone.Text.Trim();
            theLienHe.HoTen = this.txtFullName.Text.Trim();
            theLienHe.NoiDung = this.txtConten.Text.Trim();
            theLienHe.Fax = this.txtFax.Text.Trim();
            theLienHe.Email = this.txtEmail.Text.Trim();
            theLienHe.LienHeID = -1;

            LienHeProvider.IULienHe(theLienHe, true);

            // gui mail
            ThongTinCongTy theThongTinCongTy = ThongTinCongTyProvider.GetThongTinCongTy();
            String messageSendMail = "send-mail-success";

            //if (theThongTinCongTy != null)
            //{
            //    try
            //    {
            //        MailMessage theMail = new MailMessage();
            //        SmtpClient theSmtpServer = new SmtpClient("smtp.gmail.com");
            //        theSmtpServer.Port = 587;
            //        theSmtpServer.Credentials = new System.Net.NetworkCredential(theThongTinCongTy.Email, theThongTinCongTy.EmailPassword);
            //        theSmtpServer.EnableSsl = true;

            //        //thiet lap thong tin email
            //        theMail.From = new MailAddress(theThongTinCongTy.Email);
            //        theMail.To.Add(theThongTinCongTy.Email);
            //        theMail.Subject = "Có người liên hệ từ website liên hệ từ website";
            //        theMail.Body += "Tên : <b>" + txtFullName.Text + "</b>";
            //        theMail.Body += "Địa chỉ : <b>" + txtAddress.Text + "</b>";
            //        theMail.Body += "Điện thoại : <b>" + txtPhone.Text + "</b>";
            //        theMail.Body += "Fax : <b>" + txtFax.Text + "</b>";
            //        theMail.Body += "Email : <b>" + txtEmail.Text + "</b>";
            //        theMail.Body += "Nội dung : <b>" + txtConten.Text + "</b>";

            //        theSmtpServer.Send(theMail);
            //    }
            //    catch (Exception ex)
            //    {
            //        messageSendMail = "send-email-error-" + ex.Message;
            //    }

            //}

            Response.Redirect("~/LienHeSendingSuccess.aspx?message=" + messageSendMail);
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        this.txtAddress.Text = "";
        this.txtConten.Text = "";
        this.txtEmail.Text = "";
        this.txtFax.Text = "";
        this.txtFullName.Text = "";
        this.txtPhone.Text = "";
        this.txtSecurityCode.Text = "";
    }

    protected void cvSecurityCode_ServerValidate(object source, ServerValidateEventArgs args)
    {
        Boolean result = false;
        String theCapcha = Session[HangSo.CAPCHA].ToString();

        if (theCapcha.Equals(this.txtSecurityCode.Text.Trim()))
        {
            result = true;
        }

        args.IsValid = result;
    }
}
